<template>
	<view>
		<u-navbar title="登  录" :bgColor="bgColor" leftIconSize='0px' placeholder='true' titleStyle='font-weight: bold;'>
		</u-navbar>
		<view class="content">
			<view class="contentUp">
				<u-image src="@/static/register.png" mode="widthFix" width="20vw" :fade="true"></u-image>
				<!-- 图片 -->
			</view>
			<view class="contentCenter">
				欢迎来到邻里帮租
			</view>
			<view class="policy-agreement">
				<u-checkbox-group v-model="checkboxValue1" placement="column" @change="checkboxChange">
					<u-checkbox :customStyle="{marginBottom: '8px'}" v-for="(item, index) in checkboxList1" :key="index"
						:label="item.name" :name="item.name">
					</u-checkbox>
				</u-checkbox-group>
				<view style="display: flex;align-items: center;font-size: 10px;" @click="viewPolicy">
					《邻里帮租用户服务协议》
				</view>
			</view>
			<view class="contentEnd">
				<button @click="login">微信一键登录</button>
			</view>
		</view>
	</view>
</template>
<script setup>
	import {
		ref,
		reactive,
		watchEffect,
	} from 'vue';
	import {
		get,
		post
	} from '@/serve/method.js';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	const isCheckedPolicy = ref()
	// 创建响应式数据  
	const bgColor = ref('#FFF7D0');
	const checkboxValue1 = ref([]);
	const checkboxList1 = reactive([{
		name: '我已阅读并同意',
		disabled: false,
	}]);
	const checkboxChange = (n) => {
		checkboxValue1.value.push(n)
	};
	const viewPolicy = () => {
		uni.navigateTo({
			url: '/pages/index/agreement'
		})
	}
	const location = ref()
	onLoad(() => {})
	const login = () => {
		// 以下为修改
		wx.login({
			success(res) {
				if (res.code) {
					let urls = "/api/minLogin"
					get(urls, {
						code: res.code
					}).then((res) => {
						console.log(res, 88888)
						if (res.data.unionId == null) {
							console.log('新')
							location.value = '/pages/index/selection'
						} else {
							console.log('旧')
							// isCheckedPolicy.value = 'isChekcedPolicy'
							location.value = '/pages/homepage/index'
							// 修改这里
							// checkboxValue1.value.push('我已阅读并同意')
							// console.log(checkboxValue1.value, 88888)
						}
						let three = {
							"vuex_token": res.data.jwtToken,
							"vuex_user": res.data
						}
						uni.setStorage({
							key: 'lifeData',
							data: three,
							success: function(resone) {
								console.log(resone, '储存');
								if (res.data.unionId == null) {
									console.log(88)
									let urls = "/api/user/addModlueIdAndUnionId"
									post(urls, {
										"unionId": 0,
										"userId": res.data.userId
									}).then((res) => {
										console.log(res)
										uni.redirectTo({
											url: location.value
										});
									})
								} else {
									uni.switchTab({
										url: location.value
									});
								}
							}
						});
					})
				} else {
					console.log('登录失败！' + res.errMsg)
				}
			}
		})
	}
</script>
<style scoped lang="scss">
	button::after {
		display: none;
		font-size: 14px;
	}

	.content {
		background-image: linear-gradient(to bottom, #FFF7D0, #fff);
		height: 50vh;

		.contentUp {
			display: flex;
			justify-content: center;
			padding-top: 10vh;
			height: 5vh;
		}

		.contentCenter {
			text-align: center;
			font-size: 16pt;
			margin-top: 7vh;
			font-weight: bold;
		}

		.contentEnd {
			display: flex;
			justify-content: center;
			margin-top: 3vh;

			button {
				background-color: #FFDA00;
				border-radius: 50px;
				border: 0.5px solid #FFDA00;
				width: 80vw;
			}
		}

		.policy-agreement {
			width: 80vw;
			margin: 20vh auto 0px;
			text-align: center;
			font-size: 14px;
			display: flex;
			align-items: center;

			.verify {
				color: red;
			}

			checkbox-group {
				width: 80vw;
				display: flex;
				margin: 0px auto;
			}
		}
	}
</style>